<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入发布人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" style="width: 270px;">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option :label="'通过'" :value="1" />
          <el-option :label="'不通过'" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

  

    <el-table v-loading="loading" :data="reviewedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="编号" align="center" width="60" :header-cell-style="{whiteSpace: 'nowrap'}" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="发布者" align="center" prop="merchantName" />
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <el-tag 
            :type="getStatusType(scope.row.status)" 
            size="small"
            v-if="getStatusText(scope.row.status)"
          >
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['cc:reviewed:list']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="详情信息" v-model="detailVisible" width="500px" :close-on-click-modal="false">
      <el-form label-position="top" :model="detailForm" style="margin-top: 20px;">
        <el-form-item label="标题">
          <el-input v-model="detailForm.title" disabled></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="detailForm.content" type="textarea" rows="4" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="detailForm.extend1" type="textarea" rows="3" disabled></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 添加或修改已审核对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reviewedRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发布人" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="发布时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Reviewed">
import { listReviewed, getReviewed, delReviewed, addReviewed, updateReviewed } from "@/api/cc/reviewed"

const { proxy } = getCurrentInstance()

/** 将状态转换为文本 */
function getStatusText(status) {
  // 处理可能的数字或字符串类型
  const statusNum = Number(status);
  const statusMap = {
    0: '待审核',
    1: '审核通过',
    2: '审核不通过'
  }
  return statusMap[statusNum] || '未知状态'
}

/** 获取状态对应的标签类型 */
function getStatusType(status) {
  // 处理可能的数字或字符串类型
  const statusNum = Number(status);
  const typeMap = {
    0: 'warning',  // 待审核 - 橙色
    1: 'success',  // 审核通过 - 绿色
    2: 'danger'    // 审核不通过 - 红色
  }
  return typeMap[statusNum] || 'info'
}

const reviewedList = ref([])
const open = ref(false)
const detailVisible = ref(false)
const detailForm = ref({
  title: '',
  content: '',
  extend1: ''
})

/** 查看详情 */
function handleDetail(row) {
  // 将选中行的数据赋值给详情表单
  detailForm.value = {
    title: row.title || '',
    content: row.content || '',
    extend1: row.extend1 || ''
  }
  // 显示详情对话框
  detailVisible.value = true
}
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    merchantName: null,
    status: null,
    orderByColumn: 'time',
    isAsc: 'desc'
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询已审核列表 */
function getList() {
  loading.value = true
  listReviewed(queryParams.value).then(response => {
    reviewedList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    merchantName: null,
    time: null,
    status: null,
    isLaunch: null,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    extend1: null,
    extend2: null
  }
  proxy.resetForm("reviewedRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  // 确保所有查询参数都被正确重置，特别是status
  Object.assign(queryParams.value, {
    title: null,
    merchantName: null,
    status: null
  })
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加已审核"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getReviewed(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改已审核"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reviewedRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReviewed(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addReviewed(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除已审核编号为"' + _ids + '"的数据项？').then(function() {
    return delReviewed(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cc/reviewed/export', {
    ...queryParams.value
  }, `reviewed_${new Date().getTime()}.xlsx`)
}

getList()
</script>
